//
//  PKRadioMainViewController.m
//  PianKe
//
//  Created by lanou on 15/9/25.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "PKRadioMainViewController.h"
#import "PKRequestManager.h"
#import "PKRadioTableViewCell.h"
#import "PKRadioScrollerView.h"
#import "PKCarousel.h"
#import "PKhotlist.h"
#import "PKRadioDetailTableViewController.h"
#import "PKPKPlayViewController.h"

@interface PKRadioMainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *mainTableView;
@property(nonatomic,strong)NSMutableArray *dataSourceArray;
@property(nonatomic,strong)PKRadioScrollerView *scrollView;
@property(nonatomic,strong)NSMutableArray *carouselArray;
@property(nonatomic,strong)NSMutableArray *hotlistArray;
@end

@implementation PKRadioMainViewController
-(NSMutableArray *)dataSourceArray
{
    if (_dataSourceArray==nil) {
        _dataSourceArray=[NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}
-(NSMutableArray *)carouselArray
{
    if (_carouselArray==nil) {
        _carouselArray=[NSMutableArray arrayWithCapacity:0];
    }
    return _carouselArray;
}
-(NSMutableArray *)hotlistArray
{
    if (_hotlistArray==nil) {
        _hotlistArray=[NSMutableArray arrayWithCapacity:0];
        
    }
    return _hotlistArray;
}
-(void)acquireData
{
    NSMutableDictionary *parDic =[NSMutableDictionary dictionary];
    parDic[@"auth"]=@"";
    parDic[@"client"]=@"1";
    parDic[@"deviceid"]=@"63A94D37-33F9-40FF-9EBB-481182338873";

    parDic[@"version"]=@"3.0.2";
    [PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/ting/radio" patDic:parDic finish:^(NSData *data) {
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSArray *listArray=dict[@"data"][@"alllist"];
        for (NSDictionary *modelDict in  listArray) {
            PKRadioMainListModel *radioModel=[[PKRadioMainListModel alloc]init];
            [radioModel setValuesForKeysWithDictionary:modelDict];
            [self.dataSourceArray  addObject:radioModel];
            
            
        }
            _scrollView=[[PKRadioScrollerView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];

        NSArray *carouselArray =dict[@"data"][@"carousel"];
        for (NSDictionary *modelDict1 in   carouselArray) {
            PKCarousel *carrousel=[[PKCarousel alloc]init];
            [carrousel setValuesForKeysWithDictionary:modelDict1];
            [self.carouselArray addObject:carrousel];
            
        }
            _scrollView.dataSourceArray=self.carouselArray;
        NSArray *hotlistArray=dict[@"data"][@"hotlist"];
        for (NSDictionary *modelDict2 in  hotlistArray) {
            PKhotlist *hotlist=[[PKhotlist alloc]init];
            [hotlist setValuesForKeysWithDictionary:modelDict2];
            [self.hotlistArray addObject:hotlist];
            
        }
        _scrollView.hotlistArray=self.hotlistArray;
        __block PKRadioMainViewController *test=self;
        _scrollView.successTapBlcok=^(NSString *tingId)
        {
            PKPKPlayViewController *playerVC=[[PKPKPlayViewController alloc]init];
            playerVC.tingid=tingId;
            
            NSLog(@"%@",tingId);
            [test.navigationController pushViewController:playerVC  animated:YES];
        };
        _scrollView.clickButtonBlock=^(NSString *radioId,NSString *title)
        {
            PKRadioDetailTableViewController *detailVC=[[PKRadioDetailTableViewController alloc]initWithStyle:UITableViewStylePlain];
            test.navigationItem.title=title;
            detailVC.radioid=radioId;
            [test.navigationController pushViewController:detailVC animated:YES];
        };
        _scrollView.clickSecondButtonBlock=^(NSString *radioId,NSString *title)
        {
            PKRadioDetailTableViewController *detailVC=[[PKRadioDetailTableViewController alloc]initWithStyle:UITableViewStylePlain];
            test.navigationItem.title=title;
            detailVC.radioid=radioId;
            [test.navigationController pushViewController:detailVC animated:YES];
        };
        _scrollView.clickThirdButtonBlock=^(NSString *radioId,NSString *title)
        {
            PKRadioDetailTableViewController *detailVC=[[PKRadioDetailTableViewController alloc]initWithStyle:UITableViewStylePlain];
            test.navigationItem.title=title;
            detailVC.radioid=radioId;
            [test.navigationController pushViewController:detailVC animated:YES];
        };


            _mainTableView.tableHeaderView=_scrollView;


//        NSLog(@"%@",self.carouselArray);
        [_mainTableView reloadData];
    } Error:^(NSError *error) {
        NSLog(@"电台主页面网络请求失败：%@",error);
    }];
    
    
}
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleLabel.text=@"电台";
    self.topView.backgroundColor=[UIColor grayColor];
     [self acquireData];
    self.view.backgroundColor=[UIColor redColor];
//    _scrollView=[[PKRadioScrollerView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
//    _scrollView.dataSourceArray=[NSMutableArray array];
//    
//    NSLog(@"-------%@",self.carouselArray);
//    _scrollView.dataSourceArray=self.carouselArray;
  
    _mainTableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT-70)];
    _mainTableView.delegate=self;
    _mainTableView.dataSource=self;
//    _mainTableView.tableHeaderView=_scrollView;

//    _mainTableView.rowHeight=120;
    [_mainTableView registerClass:[PKRadioTableViewCell class] forCellReuseIdentifier:@"PKRadioTableViewCell "];
    [self.view addSubview:_mainTableView];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
   
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   PKRadioTableViewCell *cell =[tableView  dequeueReusableCellWithIdentifier:@"PKRadioTableViewCell " forIndexPath:indexPath];
    cell.radioMainListModel=_dataSourceArray[indexPath.row];
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKRadioMainListModel *radioModel =self.dataSourceArray[indexPath.row];
 return    [PKRadioTableViewCell heigthForCell:radioModel];
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKRadioDetailTableViewController *detailVC=[[PKRadioDetailTableViewController alloc]initWithStyle:UITableViewStylePlain];
    PKRadioMainListModel *mainListModel=self.dataSourceArray[indexPath.row];
    detailVC.radioid=mainListModel.radioid;
//    detailVC.titleName=mainListModel.title;
    self.navigationItem.title=mainListModel.title;
    [self.navigationController pushViewController:detailVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
